package cn.iocoder.yudao.module.social.service.container;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.social.controller.app.container.vo.AppContainerRespVO;
import cn.iocoder.yudao.module.social.convert.container.AppContainerConvert;
import cn.iocoder.yudao.module.social.dal.dataobject.container.ContainerDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 用户端容器转换服务
 *
 * @author 芋道源码
 */
@Service
@Slf4j
public class AppContainerConvertService {

    @Resource
    private ContainerPermissionService containerPermissionService;

    /**
     * 转换为用户端响应VO，包含权限信息
     */
    public AppContainerRespVO convertWithPermissions(ContainerDO container, Long userId) {
        if (container == null) {
            return null;
        }
        
        AppContainerRespVO respVO = AppContainerConvert.INSTANCE.convert(container);
        
        // 设置权限相关字段
        if (userId != null) {
            respVO.setIsMyContainer(Objects.equals(container.getCreatorUserId(), userId));
            respVO.setCanEdit(containerPermissionService.canEdit(container, userId));
            respVO.setCanDelete(containerPermissionService.canDelete(container, userId));
            respVO.setCanPost(containerPermissionService.canPost(container, userId));
        } else {
            respVO.setIsMyContainer(false);
            respVO.setCanEdit(false);
            respVO.setCanDelete(false);
            respVO.setCanPost(false);
        }
        
        return respVO;
    }

    /**
     * 转换列表，包含权限信息
     */
    public List<AppContainerRespVO> convertListWithPermissions(List<ContainerDO> list, Long userId) {
        if (list == null) {
            return null;
        }
        
        return list.stream()
                .map(container -> convertWithPermissions(container, userId))
                .toList();
    }

    /**
     * 转换分页结果，包含权限信息
     */
    public PageResult<AppContainerRespVO> convertPageWithPermissions(PageResult<ContainerDO> page, Long userId) {
        if (page == null) {
            return null;
        }
        
        List<AppContainerRespVO> list = convertListWithPermissions(page.getList(), userId);
        return new PageResult<>(list, page.getTotal());
    }

    /**
     * 过滤用户可见的容器并转换
     */
    public List<AppContainerRespVO> filterAndConvertList(List<ContainerDO> list, Long userId) {
        if (list == null) {
            return null;
        }
        
        return list.stream()
                .filter(container -> containerPermissionService.canView(container, userId))
                .map(container -> convertWithPermissions(container, userId))
                .toList();
    }

    /**
     * 过滤用户可见的容器并转换分页结果
     */
    public PageResult<AppContainerRespVO> filterAndConvertPage(PageResult<ContainerDO> page, Long userId) {
        if (page == null) {
            return null;
        }
        
        List<AppContainerRespVO> filteredList = filterAndConvertList(page.getList(), userId);
        return new PageResult<>(filteredList, (long) filteredList.size());
    }

}
